package com.liaoban.app.ui.base;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.adapter.MFragmentPagerAdapter;
import com.liaoban.app.util.ScreenUtils;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by ChenYuYun on 2018/3/23.
 * 默认的含有tabHost的页面模板
 */

public abstract class BaseTabHostActivity extends BaseToolbarActivity {
    @BindView(R.id.main_tab)
    public TabLayout mainTab;
    @BindView(R.id.main_viewpager)
    public ViewPager mainViewpager;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_tabhost;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        MFragmentPagerAdapter mAdapter = new MFragmentPagerAdapter(getSupportFragmentManager(), getFragmentList(), getTitleList(), getBundles());
        mainViewpager.setAdapter(mAdapter);
        mainTab.setupWithViewPager(mainViewpager);
        mainTab.setTabMode(TabLayout.MODE_FIXED);
    }


    /**
     * 设置当前选项卡的选中位置
     *
     * @param pos
     */
    public void setCurrentItem(int pos) {
        mainViewpager.setCurrentItem(pos);
    }

    public void setIndicator(int margin) {
        mainTab.post(() -> setIndicator(margin, mainTab));
    }

    /**
     * 设备tabLayout的下划线边距
     *
     * @param margin
     */
    public void setIndicator(int margin, TabLayout tabLayout) {
        try {
            //拿到tabLayout的mTabStrip属性
            Field mTabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
            mTabStripField.setAccessible(true);
            LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(tabLayout);

            int dp10 = ScreenUtils.dip2px(this, margin);

            for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                View tabView = mTabStrip.getChildAt(i);

                //拿到tabView的mTextView属性
                Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                mTextViewField.setAccessible(true);

                TextView mTextView = (TextView) mTextViewField.get(tabView);

                tabView.setPadding(0, 0, 0, 0);

                //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                int width = 0;
                width = mTextView.getWidth();
                if (width == 0) {
                    mTextView.measure(0, 0);
                    width = mTextView.getMeasuredWidth();
                }

                //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                params.width = width;
                params.leftMargin = dp10;
                params.rightMargin = dp10;
                tabView.setLayoutParams(params);

                tabView.invalidate();
            }

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取tabHost标题列表
     *
     * @return
     */
    protected abstract String[] getTitleList();

    protected abstract Bundle[] getBundles();

    /**
     * 获取tabHost下的页面集合
     *
     * @return
     */
    protected abstract Class[] getFragmentList();

}
